package com.allan.springboot_wms.controller;


import com.alibaba.fastjson.JSONObject;
import com.allan.springboot_wms.domain.User;
import com.allan.springboot_wms.req.UserQueryReq;
import com.allan.springboot_wms.req.UserSaveReq;
import com.allan.springboot_wms.resp.CommonResp;
import com.allan.springboot_wms.resp.UserLoginResp;
import com.allan.springboot_wms.resp.UserResp;
import com.allan.springboot_wms.resp.PageResp;
import com.allan.springboot_wms.service.UserService;
import com.allan.springboot_wms.util.CopyUtil;
import com.allan.springboot_wms.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;


@RestController //ResponseBody + Controller
//@Controller   返回的是页面 上面返回是字符串
@CrossOrigin    //实现跨域访问
@RequestMapping("/user")
public class UserController {
    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);
    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;
    //注入雪花算法

    @Resource
    private SnowFlake snowFlake;

    @RequestMapping("/list")
    public CommonResp list(@Valid UserQueryReq req){
        CommonResp<PageResp<UserResp>> resp = new CommonResp<>();
        PageResp<UserResp> list = userService.list(req);
        resp.setContent(list);
        return resp;
    }

    @DeleteMapping("/delete/{admin}")
    public CommonResp delete(@PathVariable String admin){
        CommonResp resp = new CommonResp<>();
        userService.delete(admin);
        return resp;
    }

    /**
     * 注意：如果前端用json格式提交的话就要写上RB注解
     *      如果是appliaction/x-www-form-urlencoded表单方式的提交就不需要＋任何注解
     *      !!!添加时不要输入id，如果输入id存在，但是name对不上，数据库会找不到主键 直接操作失败，因为两个主键是一起的。
     * @param req
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody UserSaveReq req){
        req.setPwd(DigestUtils.md5DigestAsHex(req.getPwd().getBytes()));    //作MD5加密处理
        CommonResp resp = new CommonResp<>(); //因为只要返回true就行了 基本上没有返回参数
        userService.save(req);
        return resp;
    }

    @PostMapping("/update")
    public CommonResp update(@Valid @RequestBody UserSaveReq req){
        req.setPwd(DigestUtils.md5DigestAsHex(req.getPwd().getBytes()));    //作MD5加密处理
        CommonResp resp = new CommonResp<>(); //因为只要返回true就行了 基本上没有返回参数
        userService.update(req);
        return resp;
    }

    @PostMapping("/login")
    public CommonResp login(@Valid @RequestBody UserQueryReq req){
        req.setPwd(DigestUtils.md5DigestAsHex(req.getPwd().getBytes()));    //作MD5加密处理
        CommonResp resp = new CommonResp<>();
        UserLoginResp userlogin = userService.login(req);

        Long token = snowFlake.nextId();
        LOG.info("生成单点登录token：{}, 并放入Redis中", token);
        userlogin.setToken(token.toString());
        //传入的对象需要实现序列化，因为是远程
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(userlogin), 3600*24, TimeUnit.SECONDS);
        resp.setContent(userlogin);
        return resp;
    }
    //测试是否讲其存入redis
    @RequestMapping("/redis/get/{key}")
    public Object get(@PathVariable Long key) {
        Object object = redisTemplate.opsForValue().get(key);
        LOG.info("key: {}, value: {}", key, object);
        return object;
    }

    //增加登出接口
    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable String token){
        CommonResp resp = new CommonResp<>();
        redisTemplate.delete(token); //这句写到service层也可以
        LOG.info("从redis中删除token：{}", token);
        return resp;
    }
}

